package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;

/**
 *  和讯论坛 解析类
 *
 * @author Chase
 *
 */
public class HexunBbsAnalyse implements AnalyseNews {

	

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://bbs.hexun.com/[a-z_0-9_0-9_0-9_a-z]*.html",
//				"http://[0-9]*.blog.hexun.com/[0-9]*_d.html",
//				"http://guba.hexun.com/d/[0-9]*,guba.html",
//				"http://guba.hexun.com/[0-9]*,guba,[0-9]*.html"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String url = urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
			System.err.println("不符合规则");
		}

		String html = urlMeta.getHtml();

		/*
		 * 定义新闻实体需要的字段
		 */
		String title = null;
		String dateStr = null;
		String content = null;
		String author = null;
		Long dateLong = null;
		Integer commentNum = null;
		Integer clickNum = null;

		Document doc = Jsoup.parse(html);
		Elements select = doc.select("div.warning");
		if(select.size()>0){
		}
		dateStr = doc.select("h3").text();
		if (StringUtils.isNullOrEmpty(dateStr)) {
			dateStr = doc.select("div#ArticeTextID div.ArticleTitle").text();
			if (dateStr==null || dateStr.length()<=0) {
				dateStr = doc.select("body").text();
			}
		}
		dateLong = DateUtils.matchDate(dateStr);// 时间

		title = doc.select("div#divTitle>strong").text();// 标题
		if (StringUtils.isBlank(title)) {
			title = doc.select("#BlogPageRight div#ArticeTextID div.ArticleTitle span.ArticleTitleText").text();// 标题
		}

		if (StringUtils.isBlank(title)) {
			title = doc.select("div.articleTitle").text();
		}

		content = doc.select("div.txtmain").text();
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url, doc.select("div.slide_subtitle"));
		}
		
		if (doc.select("a.f06c0").size()>0){
			author = doc.select("a.f06c0").get(0).text();
		}
		if(StringUtils.isBlank(author)){
			author = doc.select("div.articleInfo_l a").text();
		}

		String commentStr = doc.select("span.fc00").text();
		if (commentStr.indexOf(" ") != -1) {
			String[] split = commentStr.split(" ");
				if (split.length>1) {
					commentNum = NumberUtils.matchNumber(split[1]);//评论数
				}
		}
		clickNum = NumberUtils.matchNumber(doc.select("span#pv").text());

		if(clickNum==null && commentNum==null){
			String text = doc.select("div.pageText").text();
			Integer comments = NumberUtils.matchNumber(StringUtils.substringBefore(text, ","));
			Integer page = NumberUtils.matchNumber(StringUtils.substringAfter(text, ","));
			if (comments!=null){
				commentNum = comments*page;
			}
		}


		NewsMeta meta = new NewsMeta();
		meta.setTitle(title);
		meta.setContent(content);
		meta.setAuthor(author);
		meta.setUrl(url);
		meta.setType(4);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);
		meta.setDate(dateLong);

		/**
		 * 解析用于更新的地址
		 */
		String updateUrl= url;
		meta.setUpdateUrl(updateUrl);
		return meta;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(StringUtils.isNotBlank(updateUrl)){
				UrlMeta urlMeta  = CrawlHTML.responseToURL(updateUrl);
				return parserHtml(urlMeta);
			}
		}
		return null;
	}



	
}
